// 负责处理IM通讯
import GoEasy from '@/uni_modules/GOEASY-IM/js_sdk/goeasy-2.6.1.esm.min.js'// 用于通知 https://www.goeasy.io/

//用户数据示例
let users = [{
		"uuid": "08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a",
		"name": "Mattie",
		"password": "123",
		"avatar": '/static/images/Avatar-1.png'
	},
	{
		"uuid": "3bb179af-bcc5-4fe0-9dac-c05688484649",
		"name": "Wallace",
		"password": "123",
		"avatar": '/static/images/Avatar-2.png'
	},
	{
		"uuid": "fdee46b0-4b01-4590-bdba-6586d7617f95",
		"name": "Tracy",
		"password": "123",
		"avatar": '/static/images/Avatar-3.png'
	},
	{
		"uuid": "33c3693b-dbb0-4bc9-99c6-fa77b9eb763f",
		"name": "Juanita",
		"password": "123",
		"avatar": '/static/images/Avatar-4.png'
	}
];

//群数据示例
let groups = [{
		"uuid": "group-a42b-47b2-bb1e-15e0f5f9a19a",
		"name": "小程序交流群",
		"avatar": '/static/images/wx.png',
		"userList": ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', '3bb179af-bcc5-4fe0-9dac-c05688484649',
			'fdee46b0-4b01-4590-bdba-6586d7617f95', '33c3693b-dbb0-4bc9-99c6-fa77b9eb763f'
		]
	},
	{
		"uuid": "group-4b01-4590-bdba-6586d7617f95",
		"name": "UniApp交流群",
		"avatar": '/static/images/uniapp.png',
		"userList": ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', 'fdee46b0-4b01-4590-bdba-6586d7617f95',
			'33c3693b-dbb0-4bc9-99c6-fa77b9eb763f'
		]
	},
	{
		"uuid": "group-dbb0-4bc9-99c6-fa77b9eb763f",
		"name": "GoEasy交流群",
		"avatar": '/static/images/goeasy.jpeg',
		"userList": ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', '3bb179af-bcc5-4fe0-9dac-c05688484649']
	}
];


function RestApi() {

}

function User(uuid, name, avatar) {
	this.uuid = uuid;
	this.name = name;
	this.avatar = avatar;
}

function Group(uuid, name, avatar) {
	this.uuid = uuid;
	this.name = name;
	this.avatar = avatar;
}

RestApi.prototype.findFriends = function(user) {
	let friendList = users.filter(v => v.uuid !== user.uuid);
	return friendList;
}

RestApi.prototype.findGroups = function(user) {
	let groupList = groups.filter(v => v.userList.find(id => id === user.uuid));
	return groupList;
}

RestApi.prototype.findUser = function(username, password) {
	let user = users.find(user => (user.name === username && user.password === password));
	if (user) {
		return new User(user.uuid, user.name, user.avatar);
	}
	return user;
}

RestApi.prototype.findGroupById = function(groupId) {
	let group = groups.find(group => (group.uuid === groupId));
	return new Group(group.uuid, group.name, group.avatar);
};

RestApi.prototype.findUserById = function(userId) {
	let user = users.find(user => (user.uuid === userId));
	return new User(user.uuid, user.name, user.avatar);
};

RestApi.prototype.findGroupMembers = function(groupId) {
	let members = {};
	let group = groups.find(v => v.uuid === groupId);
	users.map(user => {
		let groupUserUuid = group.userList.find((uuid) => {
			return uuid === user.uuid;
		});
		if (groupUserUuid) {
			members[groupUserUuid] = new User(user.uuid, user.name, user.avatar);
		}
	});
	return members;
}

RestApi.prototype.getCurrentUser = function() {
	return {
		"uuid": "3bb179af-bcc5-4fe0-9dac-c05688484649",
		"name": "Wallace",
		"password": "123",
		"avatar": '/static/images/Avatar-2.png'
	};
}

const initIm = function(app) {
	app.config.globalProperties.GoEasy = GoEasy;
	// app.config.globalProperties.goEasy = GoEasy.getInstance({
	// 	host: 'hangzhou.goeasy.io', //应用所在的区域地址: 【hangzhou.goeasy.io |singapore.goeasy.io】
	// 	appkey: "BS-c372dd95571640a09900b96869540c87" //替换为您的应用appkey
	// });

	// app.config.globalProperties.goEasy.connect({
	// 	onSuccess() { //连接成功
	// 		console.log("GoEasy connect successfully.") //连接成功
	// 	},
	// 	onFailed(error) { //连接失败
	// 		console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error.content);
	// 	},
	// 	onProgress(attempts) { //连接或自动重连中
	// 		console.log("GoEasy is connecting", attempts);
	// 	}
	// });

	app.config.globalProperties.goEasy = GoEasy.getInstance({
		host: 'hangzhou.goeasy.io', //应用所在的区域地址: 【hangzhou.goeasy.io |singapore.goeasy.io】
		appkey: 'BC-4475e785640b495fb322da5e530a9c33', // common key,
		modules: ['im'],
		// true表示支持通知栏提醒，false则表示不需要通知栏提醒
		allowNotification: true //仅有效于app,小程序和H5将会被自动忽略
	});

	app.config.globalProperties.goEasy.onClickNotification((message) => {
		let currentUrl;
		const routes = getCurrentPages();

		if (routes && routes.length) {
			const curRoute = routes[routes.length - 1].route;
			const curParam = routes[routes.length - 1].options;
			currentUrl = '/' + curRoute + `?to=${curParam.to}`;
		}

		let newUrl;
		switch (message.toType) {
			case GoEasy.IM_SCENE.PRIVATE:
				newUrl = '/pages/chat/privateChat/privateChat?to=' + message.senderId;
				break;
			case GoEasy.IM_SCENE.GROUP:
				newUrl = '/pages/chat/groupChat/groupChat?to=' + message.groupId;
				break;
		}

		if (currentUrl !== newUrl) {
			uni.navigateTo({
				url: newUrl,
			});
		}

	});

	app.config.globalProperties.formatDate = function(t) {
		t = t || Date.now();
		let time = new Date(t);
		let str = time.getMonth() < 9 ? ('0' + (time.getMonth() + 1)) : (time.getMonth() + 1);
		str += '-';
		str += time.getDate() < 10 ? ('0' + time.getDate()) : time.getDate();
		str += ' ';
		str += time.getHours();
		str += ':';
		str += time.getMinutes() < 10 ? ('0' + time.getMinutes()) : time.getMinutes();
		return str;
	};
	app.config.globalProperties.$myIm = new RestApi();
}

export default {
	initIm
}
